package com.cheng.chengliving.commodity.controller;

import java.util.Arrays;
import java.util.Map;


import com.cheng.chengliving.commodity.entity.AttrgroupEntity;
import com.cheng.chengliving.commodity.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cheng.chengliving.commodity.entity.AttrEntity;
import com.cheng.chengliving.commodity.service.AttrService;
import com.cheng.common.utils.PageUtils;
import com.cheng.common.utils.R;

import javax.annotation.Resource;


/**
 * 商品属性表
 *
 * @author zhucheng
 * @email 1920361386@qq.com
 * @date 2022-11-28 14:49:22
 */
@RestController
@RequestMapping("commodity/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("commodity:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 1.提供一个新的api接口/方法
     * 2.列表-根据categoryId+查询条件key完成分页检索基本属性
     */
    @RequestMapping("/base/list/{categoryId}")
    //@RequiresPermissions("commodity:attr:list")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable(value = "categoryId")Long categoryId){

        PageUtils page = attrService.queryBaseAttrPage(params, categoryId);
        return R.ok().put("page", page);
    }
    /**
     * 1.提供一个新的api接口/方法
     * 2.列表-根据categoryId+查询条件key完成分页检索销售属性
     */
    @RequestMapping("/sale/list/{categoryId}")
    //@RequiresPermissions("commodity:attr:list")
    public R baseSaleList(@RequestParam Map<String, Object> params,
                          @PathVariable(value = "categoryId")Long categoryId){

        PageUtils page = attrService.querySaleAttrPage(params, categoryId);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    //装配CategoryService
    @Resource
    private CategoryService categoryService;

    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("commodity:attr:info")
    public R info(@PathVariable("attrId") Long attrId){

        AttrEntity attrEntity = attrService.getById(attrId);

        //获取到该属性组对应的categoryId
        Long categoryId = attrEntity.getCategoryId();
        //获取到与categoryId相关的id组[1,101,1001]
        Long[] cascadeCategoryId = categoryService.getCascadeCategoryId(categoryId);
        //把cascadeCategoryId设置到 attrEntity对象 的cascadeCategoryId属性上
        attrEntity.setCascadeCategoryId(cascadeCategoryId);

        return R.ok().put("attr", attrEntity);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:attr:save")
    public R save(@RequestBody AttrEntity attr){
		// attrService.save(attr); 该方法不再适用实际业务需求
//        System.out.println(attr.getAttrGroupId());
        attrService.saveAttrAndRelation(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:attr:update")
    public R update(@RequestBody AttrEntity attr){
		//attrService.updateById(attr);
        attrService.updateByAttr(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		//attrService.removeByIds(Arrays.asList(attrIds));
        attrService.deleteByAttrId(Arrays.asList(attrIds));
        return R.ok();
    }

}
